<?php

class TimesheetsController extends AppController
{
	public $components = array('Acl', 'Auth');

	public $helpers = array('Html', 'Form');

	public function index()
	{
		$this->Timesheet->recursive = 0;
		$this->set('timesheets', $this->paginate());
	}

	public function view($id = null)
	{
		if (!$id)
		{
			$this->Session->setFlash(__('Invalid Timesheet.', true));
			$this->redirect(array('action'=>'index'));
		}

		$this->set('timesheet', $this->Timesheet->read(null, $id));
	}

	public function add()
	{
		if (!empty($this->data))
		{
			$this->Timesheet->create();

			if ($this->Timesheet->save($this->data))
			{
				$this->Session->setFlash(__('The Timesheet has been saved', true));
				$this->redirect(array('action'=>'index'));
			}
			else
			{
				$this->Session->setFlash(__('The Timesheet could not be saved. Please, try again.', true));
			}
		}

		$users = $this->Timesheet->User->find('list');
		$this->set(compact('users'));
	}

	public function edit($id = null)
	{
		if (!$id && empty($this->data))
		{
			$this->Session->setFlash(__('Invalid Timesheet', true));
			$this->redirect(array('action'=>'index'));
		}

		if (!empty($this->data))
		{
			if ($this->Timesheet->save($this->data))
			{
				$this->Session->setFlash(__('The Timesheet has been saved', true));
				$this->redirect(array('action'=>'index'));
			}
			else
			{
				$this->Session->setFlash(__('The Timesheet could not be saved. Please, try again.', true));
			}
		}

		if (empty($this->data))
		{
			$this->data = $this->Timesheet->read(null, $id);
		}

		$users = $this->Timesheet->User->find('list');
		$this->set(compact('users'));
	}

	public function delete($id = null)
	{
		if (!$id)
		{
			$this->Session->setFlash(__('Invalid id for Timesheet', true));
			$this->redirect(array('action'=>'index'));
		}

		if ($this->Timesheet->del($id))
		{
			$this->Session->setFlash(__('Timesheet deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}
}

?>
